<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Wall
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Facebook.php 2011-04-26 11:18:13 michael $
 * @author     Michael
 */

/**
 * @category   Application_Extensions
 * @package    Wall
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Wall_Plugin_Service_Facebook extends Wall_Plugin_Service_Abstract {
	
	public function check(Wall_Model_Token $token) {
		$url = 'https://graph.facebook.com/me?access_token=' . $token->oauth_token;
		$content = Engine_Api::_ ()->wall ()->getUrlContent ( $url, null, true );
		
		if (! $content) {
			return false;
		}
		$profile_info = Zend_Json::decode ( $content );
		
		if (! $profile_info || isset ( $profile_info ['error'] )) {
			return false;
		}
		return true;
	
	}
	
	public function postAction(Wall_Model_Token $token, Activity_Model_Action $action, User_Model_User $user) {
		$host_url = Engine_Api::_ ()->wall ()->getHostUrl ();
		$url = "https://graph.facebook.com/me/feed?access_token=" . $token->oauth_token . "&format=json-strings";
		
		$site_title = Engine_Api::_ ()->getApi ( 'settings', 'core' )->core_general_site_title . ": ";
		
		$data = array ('message' => html_entity_decode ( $action->body ), 'name' => $site_title . 'Activity Feed', 'link' => $host_url . $action->getHref (), 'description' => '', 'picture' => $host_url . $user->getPhotoUrl ( 'thumb.icon' ) );
		
		$attachments = $action->getAttachments ();
		$attachment = (! empty ( $attachments [0] )) ? $attachments [0]->item : null;
		
		if ($attachment) {
			$data ['link'] = $host_url . $attachment->getHref ();
			$data ['description'] = $attachment->getDescription ();
			$data ['name'] = $site_title . $attachment->getTitle ();
			if (empty ( $name )) {
				$data ['name'] = $site_title . ucwords ( $attachment->getShortType () );
			}
			$photo = $attachment->getPhotoUrl ();
			if ($photo && ! preg_match ( '/fbcdn.net$/i', parse_url ( $photo, PHP_URL_HOST ) )) {
				$data ['picture'] = $host_url . $photo;
			}
		
		}
		
		$result = Engine_Api::_ ()->wall ()->getUrlContent ( $url, $data, true );
		$result = Zend_Json::decode ( $result );
		
		return $result;
	
	}
	
	public function postStatus(Wall_Model_Token $token, $message) {
		$url = "https://api.facebook.com/method/stream.publish?access_token=" . $token->oauth_token . "&format=json-strings";
		
		$result = Engine_Api::_ ()->wall ()->getUrlContent ( $url, array ('message' => $message ), true );
		$result = Zend_Json::decode ( $result );
		
		return $result;
	}
	
	public function stream(Wall_Model_Token $token, $params = array()) {
		$url = 'https://graph.facebook.com/me/home?access_token=' . $token->oauth_token . '&format=json-strings';
		
		foreach ( $params as $key => $value ) {
			$url .= "&$key=$value";
		}
		
		$content = Engine_Api::_ ()->wall ()->getUrlContent ( $url, null, true );
		
		if (! $content) {
			return false;
		}
		$stream = Zend_Json::decode ( $content );
		return $stream;
	
	}
	
/*  public function isActive()
  {
    $setting = Engine_Api::_()->getDbTable('settings', 'core');
    $clientid = $setting->getSetting('wall.service.facebook.clientid');
    $clientsecret = $setting->getSetting('wall.service.facebook.clientsecret');

    if (empty($clientid) || empty($clientsecret)){
      return false;
    }
    return true;

  }*/

}